using System;
using System.Text;
using Koolwired.Imap;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net.Mail;

namespace Koolwired.Imap
{
    [TestClass]
    public class ImapAddressTest
    {
        static ImapAddress address1;
        static ImapAddress address2;
        static ImapAddress address3;
        static string _emailaddress = "john@doe.com";
        static string _name = "John Doe";
        static string _invalidaddress = "johndoe@";
        static Encoding _encoding = Encoding.ASCII;

        [ClassInitialize]
        public static void Init(TestContext context)
        {
            address1 = new ImapAddress(_emailaddress);
            address2 = new ImapAddress(_emailaddress, _name);
            address3 = new ImapAddress(_emailaddress, _name, _encoding);
        }
        [TestMethod]
        public void CreateSimpleAddresses()
        {
            Assert.AreEqual(address1.Address, _emailaddress);
        }
        [TestMethod]
        public void CreateNormalAddress()
        {
            Assert.AreEqual(address2.Address, _emailaddress);
            Assert.AreEqual(address2.DisplayName, _name);
        }
        [TestMethod]
        public void CreateComplexAddress()
        {
            Assert.AreEqual(address3.Address, _emailaddress);
            Assert.AreEqual(address3.DisplayName, _name);
        }
        [TestMethod]
        public void ToStringSimple()
        {
            Assert.AreEqual(address1.ToString(), _emailaddress, string.Format("{0} != {1}", address1.ToString(), _emailaddress));
        }
        [TestMethod]
        public void ToStringNormal()
        {
            Assert.AreEqual(address2.ToString(), string.Format("\"{0}\" <{1}>", _name, _emailaddress));
        }
        [TestMethod]
        public void ToStringComplex()
        {
            Assert.AreEqual(address3.ToString(), string.Format("\"{0}\" <{1}>", _name, _emailaddress));
        }
        [TestMethod]
        public void ToMailAddressSimple()
        {
            Assert.AreEqual(address1.ToMailAddress(), new System.Net.Mail.MailAddress(_emailaddress));
        }
        [TestMethod]
        public void ToMailAddressNormal()
        {
            Assert.AreEqual(address2.ToMailAddress(), new System.Net.Mail.MailAddress(_emailaddress, _name));
        }
        [TestMethod]
        public void ToMailAddressComplex()
        {
            Assert.AreEqual(address3.ToMailAddress(), new System.Net.Mail.MailAddress(_emailaddress, _name, _encoding));
        }
        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionAddressSimple()
        {
            ImapAddress address = new ImapAddress(_invalidaddress);
        }
        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionAddressNormal()
        {
            ImapAddress address = new ImapAddress(_invalidaddress, _name);
        }
        [TestMethod]
        [ExpectedException(typeof(FormatException))]
        public void FormatExceptionAddressComplex()
        {
            ImapAddress address = new ImapAddress(_invalidaddress, _name, _encoding);
        }
    }
}
